package com.xkcoding.orm.mybatis.transactional;

import com.xkcoding.orm.mybatis.SpringBootDemoOrmMybatisApplicationTests;
import lombok.SneakyThrows;
import org.junit.Test;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @Author kaibing.deng
 * @Date 2024/6/21 18:46
 * @description
 **/


public class ManuallyCommitTX extends SpringBootDemoOrmMybatisApplicationTests {

    @Resource
    private PlatformTransactionManager transactionManager;

    @Test
    @SneakyThrows
    public void  manuallyCommitTX(){
        //可以将当前线程中的事务不自动提交  进行手动提交 进行数据库并发测试
        DefaultTransactionDefinition transactionDefinition= new DefaultTransactionDefinition();
        //开启事务
        TransactionStatus transaction = transactionManager.getTransaction(transactionDefinition);
        //进行事务操作
        TimeUnit.SECONDS.sleep(10);
        //提交事务
        transactionManager.commit(transaction);
    }
}
